package com.glory.eapequipmentmonitor.adapter;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.eapequipmentmonitor.R;
import com.glory.eapequipmentmonitor.bean.ContactsInfoBean;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import java.util.List;

/**
 * @author anyang
 * @date 2017/9/11
 * @desc
 */

public class ContactAdapter extends BaseQuickAdapter<ContactsInfoBean, BaseViewHolder> {

    private Context context;
    private FragmentManager fragmentManager;
    public ContactAdapter(Context context, FragmentManager fragmentManager, @Nullable List<ContactsInfoBean> data) {
        super(R.layout.adapter_contact, data);
        this.context = context;
        this.fragmentManager = fragmentManager;
    }


    @Override
    protected void convert(BaseViewHolder helper, final ContactsInfoBean item) {
        helper.setText(R.id.name_Tv,item.getCname());
        helper.setText(R.id.department_Tv,item.getDepartment());
//        Glide.with(context).load("http://oa.glorysoft.com/Resumes/"+item.getEmpid()+".jpg")
//                .skipMemoryCache(true)
//                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
//                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
//                .placeholder(R.drawable.head)
//                .error(R.drawable.head)
//                .into((ImageView) helper.getView(R.id.user_Img));
        helper.getView(R.id.iphone_Img).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FreeApi_DialogUtils.getConfirmAndCancelDialog(context, "您要打电话给" + item.getCname() + "吗？", new FreeUI_CommonCallback() {
                    @Override
                    public void onSuccess() {
                        Intent intent = new Intent(Intent.ACTION_DIAL);
                        Uri data = Uri.parse("tel:" + item.getMobile());
                        intent.setData(data);
                        context.startActivity(intent);
                    }
                }, new FreeUI_CommonCallback() {
                    @Override
                    public void onSuccess() {

                    }
                }).show(fragmentManager,"ContactAdapter");
            }
        });
        helper.getView(R.id.wechat_Img).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(copy(item.getWeixin()))
                {
                    FreeApi_ToastUtils.showFreeToast("内容已复制到粘贴板", context, true, FreeApi_StaticMembers.TOASTSHOWTIME);
                }
            }
        });
        helper.getView(R.id.email_Img).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(copy(item.getEmail()))
                {
                    FreeApi_ToastUtils.showFreeToast("内容已复制到粘贴板", context, true, FreeApi_StaticMembers.TOASTSHOWTIME);
                }
            }
        });

    }

    /**
     * 复制内容到剪切板
     *
     * @param copyStr
     * @return
     */
    private boolean copy(String copyStr) {
        try {
            //获取剪贴板管理器
            ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", copyStr);
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
